#pragma once

class http_servlet : public acl::HttpServlet
{
public:
	http_servlet(acl::socket_stream *conn, int *connect_num_);
	~http_servlet(void);

	void setAsrHandle(FUNASR_HANDLE handle);
	void setHotwords(std::shared_ptr<std::unordered_map<std::string, int>> hotwords);

protected:
	// @override
	bool doError(acl::HttpServletRequest &, acl::HttpServletResponse &);

	// @override
	bool doOther(acl::HttpServletRequest &,
				 acl::HttpServletResponse &, const char *method);

	// @override
	bool doGet(acl::HttpServletRequest &, acl::HttpServletResponse &);

	// @override
	bool doPost(acl::HttpServletRequest &, acl::HttpServletResponse &);

	// @override
	bool doWebSocket(acl::HttpServletRequest &, acl::HttpServletResponse &);

private:
	bool doPing(acl::websocket &, acl::websocket &);
	bool doPong(acl::websocket &, acl::websocket &);
	bool doClose(acl::websocket &, acl::websocket &);
	bool doMsg(acl::websocket &, acl::websocket &);
	acl::string parseMsg(acl::string Msg);

private:
	acl::string wav_format_ = "pcm";
	acl::string waveBuffer_;
	FUNASR_HANDLE asr_handle_;
	FUNASR_DEC_HANDLE decoder_handle_;
	std::vector<std::vector<float>> embedding_;
	std::shared_ptr<std::unordered_map<std::string, int>> hws_map_;
	bool doService(int type, HttpRequest &req, HttpResponse &res);
	int *connect_num = NULL;
};
